package reg.presentation;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.Date;
import java.util.Calendar;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import reg.domain.DocumentoManager;
import reg.domain.ManifestacionManager;
import reg.domain.NoticiaManager;
import reg.domain.UserManager;
import reg.domain.Usuario;
import sun.util.resources.CalendarData_es;

public class FrontController extends HttpServlet {

	private Integer contadorDocumentosNuevos;
	private HttpSession session;
	public void init() throws ServletException {
	}

	
	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);

	}
	

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	
	
	public void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		
		if (logado(request)) {
			Date f = new Date(Calendar.getInstance().getTimeInMillis());
			UserManager.getInstance(request.getParameter("user")).actualizaUltimoLogin(request.getSession(false).getAttribute("session.user").toString(), f);
			
			String resource = request.getParameter("res");
			RequestDispatcher d = request.getRequestDispatcher(resource);
			if (d != null) {
				d.forward(request, response);
			}
			System.out.println("LOGADO ACCEDIENDO A " + resource);
		} else {
			response.sendRedirect("error.jsp");
		}

	}

	private boolean logado(HttpServletRequest request) {
		boolean logado = false;

		session = request.getSession();

		String userForm = request.getParameter("user");
		String passwdForm = request.getParameter("passwd");

		
		if (session == null) {
			session = request.getSession();
			if (userForm == null || passwdForm == null
					|| userForm.length() == 0 || passwdForm.length() == 0) {
				logado = false;

			} else {
				if (valido(userForm, passwdForm)) {
					logado = true;
					
					Date ultimoLogin = UserManager.getInstance(userForm).getUsuario().getUltimoLogin();
					Integer contadorManifestacionesNuevas = ManifestacionManager.getInstance().getManifestacionesDesde(ultimoLogin).size();
					Integer contadorDocumentosNuevos = DocumentoManager.getInstance().getDocumentosDesde(ultimoLogin).size();
					Integer contadorNoticiasNuevas = NoticiaManager.getInstance().getNoticiasDesde(ultimoLogin).size();
					
					session.setAttribute("session.user", userForm);
					session.setAttribute("session.contadorManifestacionesNuevas", contadorManifestacionesNuevas.toString());
					session.setAttribute("session.contadorDocumentosNuevos", contadorDocumentosNuevos);
					session.setAttribute("session.contadorNoticiasNuevas", contadorNoticiasNuevas);
					session.setAttribute("session.ultimoLogin", ultimoLogin);

				} else {
					logado = false;
				}

			}

		} else {
			if (userForm == null || passwdForm == null) {
				logado = true;
			} else {
				if (valido(userForm, passwdForm)) {
					logado = true;
					Date ultimoLogin = UserManager.getInstance(userForm).getUsuario().getUltimoLogin();
					Integer contadorManifestacionesNuevas = ManifestacionManager.getInstance().getManifestacionesDesde(ultimoLogin).size();
					
					contadorDocumentosNuevos = DocumentoManager.getInstance().getNumeroDocsDesde(ultimoLogin);
					session.setAttribute("session.contadorDocumentosNuevos", contadorDocumentosNuevos);
					
					
					Integer contadorNoticiasNuevas = NoticiaManager.getInstance().getNoticiasDesde(ultimoLogin).size();
					int idParaRafa = UserManager.getInstance(userForm).getIdUsuario();
					
					session.setAttribute("session.user", userForm);
					session.setAttribute("session.ultimoLogin", (Object)ultimoLogin);
					session.setAttribute("session.contadorManifestacionesNuevas", contadorManifestacionesNuevas.toString());
					
					session.setAttribute("session.contadorNoticiasNuevas", contadorNoticiasNuevas);
					session.setAttribute("session.idUsuario", idParaRafa);
				} else {
					logado = false;
				}

			}
		}
		return logado;
	}

	
	public boolean valido(String userForm, String passwdForm){
		boolean res=false;
		try{
		res = UserManager.getInstance(userForm).validaUsuario(passwdForm);
		}catch(NoSuchAlgorithmException e){e.printStackTrace();}

		return res;
	}

	
}